package com.gz.mylife.app;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gz.mylife.R;
import com.gz.mylife.util.NetworkUtil;
import com.gz.mylife.util.ToastUtils;
import com.zl.smartmall.library.po.EquipmentComment;
import com.zl.smartmall.library.util.Constants;


public class EquimentCommentIssue extends BaseActivity implements TextWatcher, OnClickListener {

	private static final String TAG = EquimentCommentIssue.class.getSimpleName();
	private static final int CONTENT_LENGTH_LIMIT = 500;

	private TextView mTvHint500;
	private EditText mEtContent;
	private View mRootView;
	private ListView commentlist;
	private List<EquipmentComment> lists;
	private MyAdapter1 myadapter;
	private LayoutInflater mInflater;
	private LinearLayout comments;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mRootView = getLayoutInflater().inflate(R.layout.activity_equipment_comment, null);
		setContentView(mRootView);

		findViewById(R.id.btn_submit).setOnClickListener(this);
		//		mActionBar.setTitle("发表评价");
		setActionbarTitle("发表评价", true);
		mInflater = getLayoutInflater();
		mTvHint500 = (TextView) findViewById(R.id.tv_hint_500);
		mEtContent = (EditText) findViewById(R.id.et_content);
		mEtContent.addTextChangedListener(this);
		mEtContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(CONTENT_LENGTH_LIMIT)});
		lists = new ArrayList<EquipmentComment>();
		lists.clear();
		lists=(ArrayList<EquipmentComment>)getIntent().getSerializableExtra("comments");

		commentlist = (ListView) findViewById(R.id.commentslist);
		comments = (LinearLayout) findViewById(R.id.comments);
		if (lists.size()>0) {
			comments.setVisibility(View.VISIBLE);
			myadapter = new MyAdapter1();
			commentlist.setAdapter(myadapter);
		}else{
			comments.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.product_comment_issue, menu);
		MenuItemCompat.setShowAsAction(menu.findItem(R.id.action_submit), 
				MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_submit:
			submit();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		mTvHint500.setText("还可以输入" + (CONTENT_LENGTH_LIMIT - mEtContent.getText().toString().length()) + "个字");
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {

		case R.id.btn_submit:
			break;


		}
	}

	private void submit() {
		if(TextUtils.isEmpty(mEtContent.getText().toString())) {
			ToastUtils.showToast(this, "请输入评价内容");
			return;
		}else{
			app_get_repair_evaluation();
		}
	}

	/**
	 * 报价信息
	 */
	private void app_get_repair_evaluation() {

		if (NetworkUtil.isNetworkConnected(EquimentCommentIssue.this)) {
			String url = Constants.URL_HOST_RUBY+"/android_oa/app_get_repair_evaluation";
			RequestParams params = new RequestParams(url);
			params.addParameter("repair_apply_id", getIntent().getStringExtra("repair_apply_id"));
			params.addParameter("evaluation", mEtContent.getText().toString());//报修申请单号
			params.addParameter("operator",FragmentPersonal.mUserInfo.getNickname() );//评价人
			Log.i("duzhijuan", params+"");
			x.http().get(params, new Callback.CommonCallback<String>() {


				@Override
				public void onCancelled(CancelledException arg0) {
					// TODO Auto-generated method stub
					Log.e("cancelled", arg0.toString());
				}

				@Override
				public void onError(Throwable arg0, boolean arg1) {
					// TODO Auto-generated method stub
					Log.e("onError", arg0.toString());
				}

				@Override
				public void onFinished() {
					// TODO Auto-generated method stub
					Log.e("onFinished","onFinished");
				}

				@Override
				public void onSuccess(String result) {
					// TODO Auto-generated method stub
					try {
						JSONObject jsonObject = new JSONObject(result.toString());
						if("0".equals(jsonObject.getString("ret"))){
							Toast.makeText(EquimentCommentIssue.this, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
							Intent intent = new Intent(EquimentCommentIssue.this,MyEquipments.class);
							intent.putExtra("address", getIntent().getStringExtra("address"));
							intent.putExtra("mallcomid", getIntent().getStringExtra("mallcomid"));
							startActivity(intent);
							finish();
						}else{
							Toast.makeText(EquimentCommentIssue.this, jsonObject.getString("msg"), Toast.LENGTH_LONG).show();
						}
					} catch (Exception e) {
						// TODO: handle exception
					}
				}

			});
		}else{
			Toast.makeText(EquimentCommentIssue.this, "糟糕,网络异常!", Toast.LENGTH_LONG).show();
		}

	}
	private class MyAdapter1 extends BaseAdapter {

		@Override
		public int getCount() {
			return lists.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return lists.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder1 holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.equipmentcomment_item, null);
				holder = new ViewHolder1();
				holder.operator = (TextView) convertView.findViewById(R.id.operator);
				holder.created_at = (TextView) convertView.findViewById(R.id.created_at);
				holder.evaluation = (TextView) convertView.findViewById(R.id.evaluation);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder1) convertView.getTag();
			}
			holder.operator.setText(lists.get(position).getOperator());
			holder.created_at.setText(lists.get(position).getDetail_created_at());
			holder.evaluation.setText(lists.get(position).getEvaluation());
			return convertView;
		}
	}
	private class ViewHolder1 {
		TextView operator;
		TextView created_at;
		TextView evaluation;

	}
}
